package com.boot.learn329flyway.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * Created by Wuxiang on 2018/2/10
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void create(String name, Integer age) {
        jdbcTemplate.update("insert into user_flyway(name, age) values(?, ?)", name, age);

        System.out.println("插入数据:"+name+" "+age);
    }

    @Override
    public void deleteByName(String name) {
        jdbcTemplate.update("delete from user_flyway where name = ?", name);

        System.out.println("删除数据:"+name);
    }

    @Override
    public Integer getAllUsers() {
        Integer integer=jdbcTemplate.queryForObject("select count(1) from user_flyway", Integer.class);
        System.out.println("select all,查询到table中现有" + integer + "条数据！\n");
        return integer;
    }

    @Override
    public void deleteAllUsers() {
        System.out.println("清空表！\n");
        jdbcTemplate.update("delete from user_flyway");
    }
}
